/*
* user用户仓库vuex
* */
import {getUserInfo, saveUser} from "@/api/user";
import {getToken,setToken,removeToken} from "@/plugins/token/index"
const state={
    user:{
        // createtime:"",
        // id:0,
        // lastlogintime:"",
        // name:"",
        // password:""
    },
}
const actions={
    async saveUser(context,userInfo){
        const user=await saveUser(userInfo);
        //本地持久化存储token
        setToken(userInfo.name)
        context.commit('SAVEUSER',user);
    },
    //通过本地存储token获取用户信息
    async getUserInfo(context){
        const userName=getToken();
        console.log("getUserInfo:",userName)
        const user=await getUserInfo(userName);
        context.commit('SAVEUSER',user);
    },
    //退出登录
    logout(context){
        removeToken();
        context.commit("LOGOUT");
    }
}
const mutations={
    SAVEUSER(state,value){
        state.user = value;
    },
    LOGOUT(state){
        state.user={};
    }
}

export default{
    state,
    actions,
    mutations,
}